home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998…eptember: Technology Seed / September 98 ADC Seed CD.toast / FireWire 1.1 DR2 SDK / Source / OpenTransport / Interfaces / OpenTptModule.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-15  |  30.0 KB  |  1,010 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        OpenTptModule.h
  3.  
  4.     Contains:    headers for STREAM modules developement
  5.  
  6.     Copyright:    © 1993-1996 by Apple Computer, Inc. and Mentat Inc., all rights reserved.
  7.  
  8.     Change History (most recent first):
  9.  
  10.         <16>     2/13/96    pfl        • Fix up the align options.
  11.         <15>    12/19/95    pfl        Add PRAGMA_ALIGN_SUPPORTED
  12.         <12>    12/15/95    mjq        • Add OTScheduleDriverDeferredTask.
  13.         <11>    11/09/95    eo         Fix the OTLengthWithLeader macro.
  14.         <10>    10/23/95    mjq        • Get rid of an _AIX #if
  15.          <9>    10/12/95    mjq        • Fix GetMessageDataSize
  16.          <8>    10/08/95    mjq        • Make GetMessageDataSize method of TStreamMessage a little more
  17.                                     efficient.                                    
  18.          <7>    10/03/95    mjq        • Add mps_become_writer
  19.          <6>     9/05/95    mjq        • #include stropts.h
  20.          <5>     9/05/95    mjq        • Drop the I_OTProviderType IOCTL.
  21.          <4>     9/05/95    mjq        • Modify the API a little to OTChangePortState
  22.                                     • Rename OpenTptDev.h to OpenTptCommon.h                                    
  23.          <3>     8/30/95    mjq        • Drop kOTModPushAlways and kOTModIsLowerMux.
  24.          <2>     8/06/95    mjq        • Add kOTMinimumTimerValue to OpenTptModule.h.
  25.                                     • Add some System 7 Remove lines                                    
  26.          <1>     7/31/95    mjq        • Started over from OpenTransport project
  27.  
  28.     To Do:
  29. */
  30.  
  31. #ifndef __OPENTPTMODULE__
  32. #define __OPENTPTMODULE__
  33.  
  34. #undef KERNEL                        /* this line deleted for release disk */
  35. #define KERNEL         1                /* this line deleted for release disk */
  36. #define OTKERNEL    1
  37.  
  38. #ifndef __OPENTRANSPORT__
  39. #include <OpenTransport.h>
  40. #endif
  41. /*
  42.  * Some typedefs needed to include cred.h and mistream.h
  43.  */
  44. typedef UInt32    uint;        // this line deleted from release disk
  45. typedef UInt16    ushort;        // this line deleted from release disk
  46. typedef UInt32    ulong;        // this line deleted from release disk
  47. typedef UInt32    uid_t;
  48. typedef UInt32    gid_t;
  49. typedef UInt32    dev_t;
  50.  
  51. #ifndef _CRED_
  52. #include <cred.h>
  53. #endif
  54. #ifndef __OPENTPTCOMMON__
  55. #include <OpenTptCommon.h>
  56. #endif
  57. #ifndef _STROPTS_
  58. #include <stropts.h>
  59. #endif
  60. #ifndef _STRLOG_
  61. #include <strlog.h>
  62. #endif
  63.  
  64. #ifndef __MEMORY__
  65. #include <Memory.h>
  66. #endif
  67. #if SystemEightOrLater                            /* this line removed for System 7 release */
  68.     #ifndef __SYNCHRONIZATION__                    /* this line removed for System 7 release */
  69.     #include <Synchronization.h>                /* this line removed for System 7 release */
  70.     #endif                                        /* this line removed for System 7 release */
  71. #endif                                            /* this line removed for System 7 release */
  72.  
  73. #if defined(__MWERKS__) && GENERATING68K
  74. #pragma pointers_in_D0
  75. #endif
  76.  
  77. #if PRAGMA_ALIGN_SUPPORTED
  78.     #if GENERATING68K
  79.     #pragma options align=mac68k
  80.     #endif
  81.     #if GENERATINGPOWERPC
  82.     #pragma options align=power
  83.     #endif
  84. #endif
  85.  
  86. /*******************************************************************************
  87. ** Printing functions
  88. ********************************************************************************/
  89.  
  90. enum
  91. {
  92.     kOTPrintOnly        = 0,
  93.     kOTPrintThenStop    = 1
  94. };
  95.  
  96. #ifdef __cplusplus
  97. extern "C" {
  98. #endif
  99.  
  100. int OTKernelPrintf(int toDo, char* fmt, ...);
  101.  
  102. #ifdef __cplusplus
  103. }
  104. #endif
  105.  
  106. /*******************************************************************************
  107. ** Function to convert the "long" value that comes back in some of the
  108. ** netbufs as a result code to the equivalent OSStatus
  109. ********************************************************************************/
  110.  
  111. typedef long                    OTError;
  112.  
  113. #define GetEError(v)            ((OTUnixErr)(((v) >> 16) & 0xffff))
  114. #define GetXTIError(v)            ((OTXTIErr)((v) & 0xffff))
  115. #define MakeTPIEError(e)        ((OTError)(((((UInt16)(e)) << 16) | TSYSERR)))
  116. #define MakeDLPIEError(e)        ((OTError)(((((UInt16)(e)) << 16) | DL_SYSERR)))
  117. #define MakeXTIError(xti)        ((OTError)(xti))
  118. #define MakeOTError(xti, e)        ((OTError)((xti) | ((UInt16)(e)) << 16))
  119.  
  120. #ifdef __cplusplus
  121. extern "C" {
  122. #endif
  123.  
  124. OSStatus OTErrorToOSStatus(OTError);
  125.                     
  126. #ifdef __cplusplus
  127. }
  128. #endif
  129.  
  130. /*******************************************************************************
  131. ** Things need to be able to export a module
  132. ********************************************************************************/
  133.  
  134. /*    -------------------------------------------------------------------------
  135.     Synchronization level codes.  These are supplied to modsw_install and
  136.     stored in the appropriate tables.  sth_osr_open and
  137.     sth_ipush use these to set up synch queue subordination for new devices
  138.     and modules.
  139.     ------------------------------------------------------------------------- */
  140.  
  141.     enum
  142.     {
  143.         SQLVL_QUEUE            = 1,
  144.         SQLVL_QUEUEPAIR        = 2,
  145.         SQLVL_MODULE        = 3,
  146.         SQLVL_GLOBAL        = 4,
  147.     
  148.         SQLVL_DEFAULT        = SQLVL_MODULE
  149.     };
  150.  
  151. /*    -------------------------------------------------------------------------
  152.     The install_info structure.
  153.     ------------------------------------------------------------------------- */
  154.  
  155.     struct install_info
  156.     {
  157.         struct streamtab*    install_str;     /* Streamtab pointer.        */
  158.         UInt32                 install_flags;
  159.         UInt32                 install_sqlvl;    /* Synchronization level.    */
  160.         char*                install_buddy;    /* Shared writer list buddy */
  161.         long                ref_load;        /* Set to 0                    */
  162.         UInt32                ref_count;        /* set to 0                    */
  163.     };
  164.     
  165.     typedef struct install_info    install_info;
  166.     
  167.     //
  168.     //    Flags used in the install_flags field
  169.     //
  170.     enum
  171.     {
  172.          kOTModIsDriver            = 0x00000001,
  173.          kOTModIsModule            = 0x00000002,
  174.          
  175.          kOTModUpperIsTPI        = 0x00001000,
  176.          kOTModUpperIsDLPI        = 0x00002000,
  177.          kOTModLowerIsTPI        = 0x00004000,
  178.          kOTModLowerIsDLPI        = 0x00008000,
  179.         //
  180.         // This flag says you don't want per-context globals
  181.         //
  182.         kOTModGlobalContext        = 0x00800000,
  183.         //
  184.         // This flag is only valid if kOTModIsDriver is set
  185.         // and the driver is a PCI-card driver using the Name Registry
  186.         //
  187.         kOTModUsesInterrupts    = 0x08000000,
  188.         //
  189.         // This flag is only valid if kOTModIsDriver is set.
  190.         //
  191.         kOTModIsComplexDriver    = 0x20000000,
  192.         //
  193.         // These flags are only valid if kOTModIsModule is set.
  194.         //
  195.         kOTModIsFilter            = 0x40000000
  196.     };
  197.  
  198. /*    -------------------------------------------------------------------------
  199.     Typedef for the GetOTInstallInfo function
  200.     
  201.     Your module must export this function, and return a pointer to the
  202.     install_info structure for the module.
  203.     ------------------------------------------------------------------------- */
  204.  
  205.     typedef install_info* (* _CDECL GetOTInstallInfoProcPtr)(void);
  206.  
  207. /*    -------------------------------------------------------------------------
  208.     Typedef for the InitStreamModule function
  209.     
  210.     Your module can optionally export this function.  It will be called 
  211.     whenever your module is about to be loaded into a stream for the
  212.     first time, or if it is about to be reloaded after having been 
  213.     unloaded. Return false if your module should NOT be loaded.
  214.     For STREAMS modules, the void* parameter will be NULL.  For drivers, it
  215.     will be the same cookie parameter that was used for registering the module.
  216.     For PCI card drivers, this will be a pointer to the OTPCIInfo structure,
  217.     which can also be interpreted as a RegEntryIDPtr.
  218.     ------------------------------------------------------------------------- */
  219.     
  220.     typedef Boolean (* _CDECL InitStreamModuleProcPtr)(void*);
  221.  
  222. /*    -------------------------------------------------------------------------
  223.     Typedef for the TerminateStreamModule function
  224.     
  225.     Your module can optionally export this function.  It will be called 
  226.     whenever your module has been closed for the last time (i.e. no other 
  227.     outstanding instances of the module exist).
  228.     ------------------------------------------------------------------------- */
  229.     
  230.     typedef void (* _CDECL TerminateStreamModuleProcPtr)(void*);
  231.  
  232. /*******************************************************************************
  233. ** Equates for shared library prefixes
  234. ********************************************************************************/
  235.  
  236.     /*
  237.      * Prefixes for Open Transport driver experts.
  238.      */
  239.     #define kOTPortScannerPrefix        "ot:pScnr$"
  240.     
  241.     /*
  242.      * Interface ID for STREAMS Modules for ASLM
  243.      */
  244.     #define kOTModuleInterfaceID        kOTModulePrefix "StrmMod"
  245.  
  246. /*******************************************************************************
  247. ** Functions for dealing with major and minor device numbers
  248. ********************************************************************************/
  249.  
  250.     typedef UInt16            major_t;
  251.     typedef UInt16            minor_t;
  252.     
  253.     /* major part of a device */
  254.     #define getmajor(x)         ((major_t)(((unsigned long)(x)>>16)&0xffff))
  255.     
  256.     /* minor part of a device */
  257.     #define getminor(x)         ((minor_t)((x)&0xffff))
  258.     
  259.     /* make a device number */
  260.     #define makedev(x,y)        ((dev_t)((((dev_t)(x))<<16) | ((y) & 0xffff)))
  261.     
  262.     #define getemajor            getmajor
  263.     #define geteminor            getminor
  264.     #define makedevice            makedev
  265.     
  266.     #define    etoimajor(majnum)    (majnum)
  267.     #define    itoemajor(majnum,j)    (majnum)
  268.                                                     /* this line deleted for release disk */
  269.     #define major                BOGUS_MAJOR_MACRO    /* this line deleted for release disk */
  270.     #define minor                BOGUS_MINOR_MACRO    /* this line deleted for release disk */
  271.  
  272. /*******************************************************************************
  273. ** Some definitions
  274. ********************************************************************************/
  275.  
  276.     /*
  277.      * The first minor number that you should use for CLONEOPENs.
  278.      * Minor numbers 0 through 9 are reserved for use by the modules
  279.      * for various control streams.
  280.      */
  281.      enum
  282.      {
  283.          kFirstMinorNumber    = 10
  284.     };
  285.  
  286.     /*
  287.      * Extra OTCommand codes that may appear on your module queue.
  288.      * These are extensions to the TPI specification for Open Transport.
  289.      * T_PRIVATE_REQ is the first available TPI message number for private
  290.      * use by modules (assuming you don't want to be confused by standard
  291.      * TPI messages).
  292.      */
  293.     enum
  294.     {
  295.         T_TIMER_REQ        = 80,    // Timer event
  296.         T_MIB_REQ        = 81,    // Request module's MIB
  297.         T_MIB_ACK        = 82,    // The module's MIB is available
  298.         
  299.         T_PRIVATE_REQ    = 90    // The first private request available
  300.     };
  301.  
  302. /*******************************************************************************
  303. ** Logging Macros
  304. ********************************************************************************/
  305.  
  306. #ifdef __cplusplus
  307. extern "C" {
  308. #endif
  309.  
  310. void            OTStrlog(queue_t*, int, int, const char*);
  311.  
  312. #ifdef __cplusplus
  313. }
  314. #endif
  315. //
  316. // These are enums for the level value
  317. //
  318. enum
  319. {
  320.     kOTLvlFatal                = 0,
  321.     kOTLvlNonfatal            = 1,
  322.     kOTLvlExtFatal            = 2,
  323.     kOTLvlExtNonfatal        = 3,
  324.     kOTLvlUserErr            = 4,
  325.     kOTLvlInfoErr            = 5,
  326.     kOTLvlInfoOnly            = 6
  327. };
  328.  
  329. #define STRLOG(q, lvl, flags, str)        OTStrlog(q, lvl, flags, str)
  330.  
  331. #if qDebug
  332.     #define STRLOG1(q, lvl, flags, str)    OTStrlog(q, lvl, flags, str)
  333. #else
  334.     #define STRLOG1(q, lvl, flags, str)
  335. #endif
  336.  
  337. #if qDebug > 1 || qDebug2 > 1
  338.     #define STRLOG2(q, lvl, flags, str)    OTStrlog(q, lvl, flags, str)
  339. #else
  340.     #define STRLOG2(q, lvl, flags, str)
  341. #endif
  342.  
  343. /*******************************************************************************
  344. ** Structure definitions
  345. **
  346. ** These go along with the extra definitions above.
  347. ********************************************************************************/
  348.  
  349.     struct  T_MIB_req
  350.     {
  351.         long    PRIM_type;        /* Always T_MIB_REQ */
  352.     };
  353.     
  354.     struct  T_MIB_ack
  355.     {
  356.         long    PRIM_type;        /* Always T_MIB_ACK     */
  357.         long    MIB_length;        /* MIB length             */        
  358.         long    MIB_offset;        /* MIB Offset            */        
  359.     };
  360.     
  361.     struct T_stream_timer
  362.     {
  363.         long        PRIM_type;    /* Always T_TIMER_REQ */
  364.         union
  365.         {
  366.             long    USER_long;
  367.             void*    USER_ptr;
  368.     #ifndef __cplusplus
  369.         }            USER_data;
  370.     #else
  371.         };
  372.     #endif
  373.     };
  374.  
  375. /*    -------------------------------------------------------------------------
  376.     Internal port record structures
  377.     ------------------------------------------------------------------------- */
  378.  
  379.     /*    
  380.      * One TPortRecord is created for each instance of a port.
  381.      * For Instance 'enet' identifies an ethernet port.
  382.      * An TPortRecord for each ethernet card it finds, with an
  383.      * OTPortRef that will uniquely allow the ethernet driver to determine which
  384.      * port it is supposed to open on.
  385.      */
  386.  
  387.     typedef struct TPortRecord    TPortRecord;
  388.  
  389.     struct TPortRecord
  390.     {
  391.         OTLink                    fLink;
  392.         char*                    fPortName;
  393.         char*                    fModuleName;
  394.         char*                    fResourceInfo;
  395.         char*                    fSlotID;
  396.         struct TPortRecord*        fAlias;
  397.         size_t                    fNumChildren;
  398.         OTPortRef*                fChildPorts;
  399.         UInt32                    fPortFlags;
  400.         UInt32                    fInfoFlags;
  401.         UInt32                    fCapabilities;
  402.         OTPortRef                fRef;
  403.         struct streamtab*        fStreamtab;
  404.         void*                    fContext;
  405.         void*                    fExtra;
  406.     };
  407.  
  408. /*******************************************************************************
  409. ** "C" Interface routines
  410. ********************************************************************************/
  411.  
  412. #ifdef __cplusplus
  413. extern "C" {
  414. #endif
  415.  
  416. /*    -------------------------------------------------------------------------
  417.     Interrupt control functions
  418.     ------------------------------------------------------------------------- */
  419.  
  420. #if USES68KINLINES
  421.     //
  422.     // MPS_INTR_STATE saves the current interrupt state
  423.     // Its definition undoubtably changes with the definition
  424.     // of mps intr enable/disable, so this typedef is
  425.     // also inside the USE68KINLINES conditional.
  426.     //
  427.     typedef UInt8    MPS_INTR_STATE;
  428.     //
  429.     // Disable interrupts and save the state
  430.     //
  431.     #pragma parameter mps_intr_disable(__A0)
  432.     void mps_intr_disable(MPS_INTR_STATE*) =
  433.     {
  434.         0x40C0,             /*        move    sr,d0        */
  435.         0xE040,                /*        asr.w    #8,d0        */
  436.         0x007C, 0x0600,        /*        ori        #$600,sr    */
  437.         0x027C, 0xFEFF,     /*        andi    #$FEFF,sr    */
  438.         0x1080                /*         move.b d0,(a0)         */
  439.     #if qDebug
  440.         , 0x0200, 0x0070,    /*        andi.b    #$70,d0        */
  441.         0x0c00, 0x0070,        /*        cmpi.b    #$70,d0        */
  442.         0x6602,                /*        bne.s    @3            */
  443.         0xA9FF                /*        DebugBreak            */
  444.     #endif
  445.     };
  446.     //
  447.     // Enable interrupts from the saved state
  448.     //
  449.     #pragma parameter mps_intr_enable(__A0)
  450.     void mps_intr_enable(MPS_INTR_STATE* ptr) =
  451.     {
  452.     #if qDebug
  453.         0x40C0,             /*        move    sr,d0        */
  454.         0x0240, 0x0700,        /*        andi.w    #$700,d0    */
  455.         0x0C40, 0x0600,        /*        cmpi.w    #$600,d0    */
  456.         0x6702,                /*         beq.s    @2             */
  457.         0xA9FF,                /*         DebugBreak             */
  458.     #endif
  459.         0x1010,                /* @2    move.b    (a0),d0        */
  460.     #if qDebug
  461.         0x0200, 0x0070,        /*        andi.b    #$70,d0        */
  462.         0x0c00, 0x0070,        /*        cmpi.b    #$70,d0        */
  463.         0x6602,                /*        bne.s    @3            */
  464.         0xA9FF,                /*        DebugBreak            */
  465.         0x1010,                /* @3    move.b    (a0),d0        */
  466.     #endif
  467.         0xE140,                /*        asl.w    #8,d0        */
  468.         0x46C0                /*        move    d0,sr         */
  469.     };
  470. #else
  471.     #if SystemEightOrLater                                            /* this line removed for System 7 release */
  472.                                                                     /* this line removed for System 7 release */
  473.         typedef InterruptState    MPS_INTR_STATE;                        /* this line removed for System 7 release */
  474.                                                                     /* this line removed for System 7 release */
  475.         #define mps_intr_disable(x)        *(x) = DisableInterrupts()    /* this line removed for System 7 release */
  476.         #define mps_intr_enable(x)        RestoreInterrupts(*x)        /* this line removed for System 7 release */
  477.                                                                     /* this line removed for System 7 release */
  478.     #else                                                            /* this line removed for System 7 release */
  479.     
  480.         typedef UInt8    MPS_INTR_STATE;
  481.  
  482.         void mps_intr_disable(MPS_INTR_STATE*);
  483.         void mps_intr_enable(MPS_INTR_STATE*);
  484.                                                                     /* this line removed for System 7 release */
  485.     #endif                                                            /* this line removed for System 7 release */
  486.  
  487. #endif
  488.  
  489. /*    -------------------------------------------------------------------------
  490.     The kOTPortScannerInterfaceID define is what you need to add to your
  491.     export file for the "interfaceID = " clause.  
  492.     PortScanProcPtr is the typedef for the scanning function.
  493.     Your port-scanning function must be exported by the name "OTScanPorts".
  494.     Your port-scanning function set must use the prefix kOTPortScannerPrefix.
  495.     ------------------------------------------------------------------------- */
  496.  
  497.     #define kOTPortScannerInterfaceID            kOTKernelPrefix "pScnr,1.0"
  498.     #define kOTPseudoPortScannerInterfaceID        kOTKernelPrefix "ppScnr,1.0"
  499.     
  500.     typedef void (* _CDECL PortScanProcPtr)(UInt32 scanType);
  501.  
  502.     enum    /* scanType */
  503.     {
  504.         kOTInitialScan            = 0,
  505.         kOTScanAfterSleep        = 1
  506.     };
  507.             
  508. /*    -------------------------------------------------------------------------
  509.     Port Registration
  510.     
  511.     These routines can be used to register, find and iterate through the
  512.     various ports on the machine
  513.     ------------------------------------------------------------------------- */
  514.         //
  515.         // Find the TPortRecord for a given Port Name
  516.         //
  517.     TPortRecord*    OTFindPort(const char* portName);
  518.         //
  519.         // Find the "nth" TPortRecord
  520.         //
  521.     TPortRecord*    OTGetIndexedPort(size_t index);
  522.         //
  523.         // Find another port that is active and conflicts with
  524.         // the port described by "ref"
  525.         //
  526.     TPortRecord*    OTFindPortConflict(OTPortRef ref);
  527.         //
  528.         // Other ways of finding the port
  529.         //
  530.     TPortRecord*    OTFindPortByRef(OTPortRef);
  531.     TPortRecord*    OTFindPortByDev(dev_t);
  532.         //
  533.         // Memory allocation for OTRegisterPort persistent data
  534.         //
  535.     void*            OTAllocPortMem(size_t);
  536.     void            OTFreePortMem(void*);
  537.     
  538. /*    -------------------------------------------------------------------------
  539.     Timer functions
  540.     ------------------------------------------------------------------------- */
  541.  
  542.     enum
  543.     {
  544.         kOTMinimumTimerValue    = 8        // 8 milliseconds is the minimum timeout value
  545.     };
  546.     
  547.     mblk_t*            mi_timer_q_switch(mblk_t*, queue_t*, mblk_t*);
  548.     Boolean            mi_timer_cancel(mblk_t*);
  549.     void            mi_timer(mblk_t*, unsigned long);
  550.     mblk_t*            mi_timer_alloc(queue_t*, size_t);
  551.     void            mi_timer_free(mblk_t*);
  552.     Boolean            mi_timer_valid(mblk_t*);
  553.  
  554. /*    -------------------------------------------------------------------------
  555.     Miscellaneous helpful routines
  556.     ------------------------------------------------------------------------- */
  557.     /*
  558.      * This routine is used by a driver at interrupt time to schedule
  559.      * a deferred task or SWI to run their interrupt processing code.
  560.      */
  561.      Boolean OTScheduleDriverDeferredTask(long dtCookie);
  562.     /*
  563.      * This is the typedef for a function that will be called when a message
  564.      * created by OTAllocMsg is destroyed.
  565.      */
  566.     typedef void    (* _CDECL EsbFreeProcPtr)(char* arg);
  567.     
  568.     /*
  569.      * This function creates a message which points to "size" bytes of data
  570.      * at "buf".  When the message is freed, the EsbFreeProcPtr function "func"
  571.      * will be called with the argument "arg".
  572.      * NOTE: This function allows users of your buffer to modify the buffer.
  573.      */
  574.     mblk_t* OTAllocMsg(void* buf, size_t size,  EsbFreeProcPtr func, void* arg);
  575.  
  576.     /*
  577.      * Routines to allocate and free memory in your modules (these are
  578.      * interrupt-time safe!). 
  579.      */
  580.  
  581.     void*    OTAllocMem(size_t);
  582.     void    OTFreeMem(void*);
  583.     void*    OTReallocMem(void* ptr, size_t newSize);
  584.  
  585.     /*
  586.      * Routines to calculate various sizes of STREAM messages
  587.      */
  588.     #define HEAD_SIZE(mp)    ((mp)->b_rptr - (mp)->b_datap->db_base)
  589.     #define TAIL_SIZE(mp)    ((mp)->b_datap->db_lim - (mp)->b_wptr)
  590.     #define MBLK_SIZE(mp)    ((mp)->b_wptr - (mp)->b_rptr)
  591.     #define DBLK_SIZE(mp)    ((mp)->b_datap->db_lim - (mp)->b_datap->db_base)
  592.     
  593.     #ifdef __cplusplus
  594.     extern "C" {
  595.     #endif
  596.     
  597.     int        mi_bcmp(const char* first, const char* second, size_t nBytes);
  598.     int        mi_sprintf (char * buf, char * fmt, ...);
  599.     
  600.     #ifdef __cplusplus
  601.     }
  602.     #endif
  603.     
  604.     #define bcopy(s, d, l)            OTMemcpy(d, s, l)
  605.     #define bzero(d, l)                OTMemzero(d, (size_t)(l))
  606.     #define bcmp(s, d, l)            mi_bcmp(s, d, l)
  607.  
  608.     /*
  609.      * Standard STREAMS bcopy, bzero, & bcmp take char* parameters.
  610.      * The BCOPY, BZERO, and BCMP routines take void* so that we do not have
  611.      * to cast all the pointers.
  612.      */
  613.     #define BCOPY(s, d, l)            bcopy((const char*)(s), (char*)(d), l)
  614.     #define BZERO(d, l)                bzero((char*)(d), l)
  615.     #define BCMP(s, d, l)            mi_bcmp((const char*)(s), (const char*)(d), l)
  616.  
  617.     /*
  618.      * Create sprintf and printf functions that will work in STREAM modules.
  619.      * Also, make sure that calling traditional "C" allocation routines
  620.      * will not compile.
  621.      */
  622.     #define sprintf                        mi_sprintf
  623.     #define printf                        OTKernelPrintf
  624.  
  625.     #define calloc                        (DONT_CALL*THIS_FUNCTION)
  626.     #define malloc                        (DONT_CALL*THIS_FUNCTION)
  627.     #define realloc                        (DONT_CALL*THIS_FUNCTION)
  628.     #define free                        (DONT_CALL*THIS_FUNCTION)
  629.  
  630.     /*
  631.      * A few other miscellaneous functions
  632.      */
  633.     int            drv_priv(struct cred* credp);
  634.     queue_t*    mi_allocq(struct streamtab*);
  635.     mblk_t*        mi_reallocb(mblk_t*, size_t new_Size);
  636.     
  637. #ifdef __cplusplus
  638. }
  639. #endif
  640.  
  641. /*******************************************************************************
  642. ** Some helpful utilites from Mentat
  643. ********************************************************************************/
  644.  
  645. #define    MI_COPY_IN        1
  646. #define    MI_COPY_OUT        2
  647. #define    MI_COPY_DIRECTION(mp)                             \
  648.     (((UInt8*)&(mp)->b_cont->b_prev)[0])
  649. #define    MI_COPY_COUNT(mp)                     \
  650.     (((UInt8*)&(mp)->b_cont->b_prev)[1])
  651. #define    MI_COPY_RVAL(mp)    (*(int *)&(mp)->b_cont->b_next)
  652. #define    MI_COPY_CASE(dir,cnt)    ((int)(((cnt)<<2)|dir))
  653. #define    MI_COPY_STATE(mp)                             \
  654.     ((int)MI_COPY_CASE(MI_COPY_DIRECTION(mp),MI_COPY_COUNT(mp)))
  655.  
  656. #ifdef __cplusplus
  657. extern "C" {
  658. #endif
  659.  
  660. typedef void    (*OTWriterProcPtr)(queue_t*, mblk_t*);
  661.  
  662. void    mps_become_writer(queue_t* q, mblk_t* mp, OTWriterProcPtr proc);
  663.  
  664. mblk_t*    mi_tpi_ack_alloc(mblk_t* mp, size_t size, long);
  665. mblk_t*    mi_tpi_conn_con(mblk_t* trailer_mp, char* src, size_t src_length, char* opt, size_t opt_length);
  666. mblk_t*    mi_tpi_conn_ind(mblk_t* trailer_mp, char* src, size_t src_length, char* opt, size_t opt_length, int seqnum);
  667. mblk_t*    mi_tpi_conn_req(mblk_t* trailer_mp, char* dest, size_t dest_length, char* opt, size_t opt_length);
  668. mblk_t*    mi_tpi_data_ind(mblk_t* trailer_mp, int flags, long type);
  669. mblk_t*    mi_tpi_data_req(mblk_t* trailer_mp, int flags, long type);
  670. mblk_t*    mi_tpi_discon_ind(mblk_t* trailer_mp, int reason, int seqnum);
  671. mblk_t*    mi_tpi_discon_req(mblk_t* trailer_mp, int seqnum);
  672. mblk_t*    mi_tpi_err_ack_alloc(mblk_t* mp, int tlierr, int unixerr);
  673. mblk_t*    mi_tpi_exdata_ind(mblk_t* trailer_mp, int flags, long type);
  674. mblk_t*    mi_tpi_exdata_req(mblk_t* trailer_mp, int flags, long type);
  675. mblk_t*    mi_tpi_info_req(void);
  676. mblk_t*    mi_tpi_ok_ack_alloc(mblk_t* mp);
  677. mblk_t*    mi_tpi_ordrel_ind(void);
  678. mblk_t*    mi_tpi_ordrel_req(void);
  679. mblk_t*    mi_tpi_uderror_ind(char* dest, size_t dest_length, char* opt, size_t opt_length, int error);
  680. mblk_t*    mi_tpi_unitdata_ind(mblk_t* trailer_mp, char* src, size_t src_length, char* opt, size_t opt_length);
  681. mblk_t*    mi_tpi_unitdata_req(mblk_t* trailer_mp, char* dst, size_t dst_length, char* opt, size_t opt_length);
  682.  
  683. mblk_t* mi_reallocb(mblk_t* old_mp, size_t new_size);
  684.  
  685. Boolean    mi_set_sth_hiwat(queue_t* q, size_t size);
  686. Boolean    mi_set_sth_lowat(queue_t* q, size_t size);
  687. Boolean    mi_set_sth_maxblk(queue_t* q, size_t size);
  688. Boolean    mi_set_sth_wroff(queue_t* q, size_t size);
  689.  
  690. UInt8* mi_offset_param(mblk_t* mp, long offset, long len);
  691. UInt8* mi_offset_paramc(mblk_t* mp, long offset, long len);
  692.  
  693. char* mi_open_detached(char** mi_opp_orig, size_t size, dev_t* devp);
  694. int mi_open_comm(char** mi_opp_orig, size_t size, queue_t* q, dev_t* dev,
  695.                  int flag, int sflag, cred_t* credp);
  696. int mi_close_comm(char** mi_opp_orig, queue_t* q);
  697. void mi_bufcall(queue_t* q, size_t size, int pri);
  698. void mi_detach(queue_t* q, char* ptr);
  699. void mi_close_detached(char** mi_opp_orig, char* ptr);
  700. char* mi_next_ptr(char*);
  701.  
  702. void mi_copyin(queue_t* q, mblk_t* mp, char* uaddr, size_t len);
  703. void mi_copyout(queue_t* q, mblk_t* mp);
  704. mblk_t* mi_copyout_alloc(queue_t* q, mblk_t* mp, char* uaddr, size_t len);
  705. void mi_copy_done(queue_t* q, mblk_t* mp, int err);
  706. void mi_copy_set_rval(mblk_t* mp, int rval);
  707. int mi_copy_state(queue_t* q, mblk_t* mp, mblk_t** mpp);
  708.  
  709. #ifdef __cplusplus
  710. }
  711.  
  712. /*******************************************************************************
  713. ** Everything below here is C++ ONLY
  714. ********************************************************************************/
  715.  
  716. /*    -------------------------------------------------------------------------
  717.     TStreamQueue class
  718.     
  719.     This class is just a convenient interface to the queue structure
  720.     ------------------------------------------------------------------------- */
  721.  
  722.     class TStreamQueue : public queue
  723.     {
  724.         public:
  725.             void        EnableQueue();
  726.     };
  727.     
  728.     /*    -------------------------------------------------------------------------
  729.         Inline methods for TStreamQueue
  730.         ------------------------------------------------------------------------- */
  731.     
  732.         inline void TStreamQueue::EnableQueue()
  733.         {
  734.             if ( q_flag & QNOENB )
  735.             {
  736.                 enableok(this);
  737.                 if ( q_first )
  738.                     qenable(this);
  739.             }
  740.         }
  741.  
  742. /*    -------------------------------------------------------------------------
  743.     Class TStreamMessage
  744.     
  745.     This class is a C++ interface to the mblk_t structure defined in STREAMS
  746.     ------------------------------------------------------------------------- */
  747.     //
  748.     // The maximum stream buffer size is the largest "size_t" that
  749.     // doesn't look negative if interpreted as a signed number
  750.     //
  751.     enum
  752.     {
  753.         kMaxStreamBufferSize    = (((size_t)-1L) >> 1)
  754.     };
  755.  
  756. #if PRAGMA_ALIGN_SUPPORTED
  757. #pragma options align=reset
  758. #pragma options align=mac68k
  759. #endif
  760.  
  761.     class TStreamMessage : public msgb
  762.     {
  763.         private:
  764.                 void*        operator new (size_t)    { return NULL; }
  765.                 
  766.         public:
  767.                 void*        operator new(size_t, size_t size)
  768.                     { return (TStreamMessage*)allocb(size, 0); }
  769.                 void*        operator new(size_t, void* buf, size_t size,
  770.                                          EsbFreeProcPtr func, void* arg)
  771.                     { return OTAllocMsg(buf, size, func, arg); }
  772.                 void        operator delete(void* ptr)
  773.                     { freemsg((mblk_t*)ptr); }
  774.     
  775.                     void            Reset(size_t = 0);
  776.                     void            ResetWithLeader(size_t);
  777.             
  778.                     void            FreeData();
  779.                     TStreamMessage*    RemoveData();
  780.                     void            AppendData(TStreamMessage* data);
  781.             
  782.                     size_t            GetSize() const;
  783.                     size_t            GetDataSize() const;
  784.                     size_t            GetMessageDataSize() const;
  785.                     void            SetDataSize(size_t);
  786.         
  787.                     void            SetType(UInt8);
  788.                     UInt8            GetType() const;
  789.                     TStreamMessage*    GetNextBlock() const;
  790.                     void            SetNextBlock(TStreamMessage*);
  791.                     char*            GetDataPointer() const;
  792.                     Boolean            IsReuseable(size_t size) const;
  793.             
  794.                     void            HideBytesAtFront(size_t);
  795.                     void            HideBytesAtEnd(size_t);
  796.             //
  797.             // The following functions ignore non-M_DATA blocks.
  798.             //
  799.                     const TStreamMessage*
  800.                                     _MDECL GetBlockAt(size_t& offset) const;
  801.                     const void*        _MDECL GetPointerTo(size_t offset, size_t* len) const;
  802.                     const void*        _MDECL GetPointerTo(size_t offset, size_t* len, void* bfr) const;
  803.             //
  804.             // This function makes a non-shared copy of any initial non-M_DATA block, and
  805.             // then makes shared copies of the requested data.  It only assumes 1 leading
  806.             // non-M_DATA block, but will skip any other non-M_DATA blocks while 
  807.             // searching for data to copy.
  808.             //
  809.                     TStreamMessage*    _MDECL MakeSharedCopy(size_t offset = 0, 
  810.                                                           size_t len = kMaxStreamBufferSize);
  811.  
  812.                     Boolean            _MDECL HasData() const;
  813.                     Boolean            _MDECL WriteData(void* buf, size_t len);
  814.             //
  815.             // These hide/remove the part of the data already read.  
  816.             //
  817.                     TStreamMessage*    _MDECL ReadData(void* buf, size_t* len);
  818.                     size_t            _MDECL ReadControl(void* buf, size_t len);
  819.     };
  820.     
  821.     /*    -------------------------------------------------------------------------
  822.         Inline methods for TStreamMessage
  823.         ------------------------------------------------------------------------- */
  824.     
  825.         inline void TStreamMessage::Reset(size_t size)
  826.         {
  827.             b_rptr = b_datap->db_base;
  828.             b_wptr = b_rptr + size;
  829.         }
  830.         
  831.         inline size_t OTLengthWithLeader(size_t size)
  832.         {
  833.             return (size + 3) & ~3;
  834.         }
  835.         
  836.         inline void TStreamMessage::ResetWithLeader(size_t size)
  837.         {
  838.             b_rptr = b_datap->db_lim - OTLengthWithLeader(size);
  839.             b_wptr = b_rptr + size;
  840.         }
  841.         
  842.         inline void TStreamMessage::FreeData()
  843.         {
  844.             if ( b_cont )
  845.             {
  846.                 freemsg(b_cont);
  847.                 b_cont = NULL;
  848.             }
  849.         }
  850.         
  851.         inline TStreamMessage* TStreamMessage::RemoveData()
  852.         {
  853.             TStreamMessage* temp = (TStreamMessage*)b_cont;
  854.             b_cont = NULL;
  855.             return temp;
  856.         }
  857.         
  858.         inline void TStreamMessage::AppendData(TStreamMessage* mp)
  859.         {
  860.             linkb(this, mp);
  861.         }
  862.     
  863.         inline size_t TStreamMessage::GetSize() const
  864.         {
  865.             return b_datap->db_lim - b_datap->db_base;
  866.         }
  867.         
  868.         inline size_t TStreamMessage::GetDataSize() const
  869.         {
  870.             return b_wptr - b_rptr;
  871.         }
  872.         
  873.         inline size_t TStreamMessage::GetMessageDataSize() const
  874.         {
  875.             return (b_cont == NULL) ? 
  876.                 (b_datap->db_type == M_DATA ? (b_wptr - b_rptr) : 0) : msgdsize(this);
  877.         }
  878.     
  879.         inline void TStreamMessage::SetDataSize(size_t size)
  880.         {
  881.             b_wptr = b_rptr + size;
  882.         }
  883.     
  884.         inline void TStreamMessage::SetType(UInt8 type)
  885.         {
  886.             b_datap->db_type = type;
  887.         }
  888.     
  889.         inline unsigned char TStreamMessage::GetType() const
  890.         {
  891.             return b_datap->db_type;
  892.         }
  893.         
  894.         inline TStreamMessage* TStreamMessage::GetNextBlock() const
  895.         {
  896.             return (TStreamMessage*)b_cont;
  897.         }
  898.         
  899.         inline void TStreamMessage::SetNextBlock(TStreamMessage* mp)
  900.         {
  901.             b_cont = (mblk_t*)mp;
  902.         }
  903.         
  904.         inline char* TStreamMessage::GetDataPointer() const
  905.         {
  906.             return (char*)b_rptr;
  907.         }
  908.         
  909.         inline Boolean TStreamMessage::IsReuseable(size_t size) const
  910.         {
  911.             return (b_datap->db_ref == 1 && GetSize() >= size);
  912.         }
  913.         
  914.         inline void TStreamMessage::HideBytesAtFront(size_t len)
  915.         {
  916.             adjmsg(this, len);
  917.         }
  918.         
  919.         inline void TStreamMessage::HideBytesAtEnd(size_t len)
  920.         {
  921.             adjmsg(this, -len);
  922.         }
  923.  
  924. /*    -------------------------------------------------------------------------
  925.     TTimerMessage class
  926.     
  927.     This class implements an interface to the STREAM environment timer
  928.     facilities.  A TTimerMessage will be placed on the queue of your choice
  929.     when it's timer expires.
  930.     ------------------------------------------------------------------------- */
  931.  
  932. #if GENERATINGPOWERPC
  933.     #define TIMER_BUG    1
  934. #else
  935.     #define TIMER_BUG    0
  936. #endif
  937.  
  938.     class TTimerMessage : public TStreamMessage
  939.     {
  940.     #if TIMER_BUG
  941.         private:
  942.     #else
  943.         public:
  944.     #endif
  945.             inline void* operator new(size_t, queue_t* q)
  946.                 {    return mi_timer_alloc(q, sizeof(T_stream_timer)); }
  947.             inline void* operator new(size_t, queue_t* q, size_t extra)
  948.                 {    return mi_timer_alloc(q, extra + sizeof(T_stream_timer)); }
  949.     
  950.             inline void operator delete(void* ptr)
  951.                 {    mi_timer_free((mblk_t*)ptr); }
  952.     
  953.         public:        
  954.                     Boolean            IsValid();
  955.                     TTimerMessage*    ChangeQueue(TStreamQueue* newQ, TTimerMessage* newMP);
  956.                     Boolean            Cancel();
  957.                     void            Schedule(OTTimeout time);
  958.     
  959.         private:
  960.             inline void* operator new(size_t)
  961.                 {    return 0; }
  962.     };
  963.     
  964.     inline TTimerMessage* NewTimerMsg(queue_t* q, size_t extra = 0)
  965.     {
  966.         return (TTimerMessage*)mi_timer_alloc(q, sizeof(T_stream_timer) + extra);
  967.     }
  968.     
  969.     inline void FreeTimerMsg(TTimerMessage* msg)
  970.     {
  971.         mi_timer_free(msg);
  972.     }
  973.     
  974.     /*    -------------------------------------------------------------------------
  975.         Inline methods for TStreamTimer
  976.         ------------------------------------------------------------------------- */
  977.     
  978.         inline Boolean TTimerMessage::IsValid()
  979.         {
  980.             return mi_timer_valid(this);
  981.         }
  982.         
  983.         inline TTimerMessage* TTimerMessage::ChangeQueue(TStreamQueue* q, TTimerMessage* newMP)
  984.         {
  985.             return( (TTimerMessage*) mi_timer_q_switch(this, q, newMP));
  986.         }
  987.     
  988.         inline Boolean TTimerMessage::Cancel()
  989.         {
  990.             return mi_timer_cancel(this);
  991.         }
  992.         
  993.         inline void TTimerMessage::Schedule(OTTimeout time)
  994.         {
  995.             ((T_stream_timer*)b_rptr)->PRIM_type = T_TIMER_REQ;
  996.             mi_timer(this, (unsigned long)time);
  997.         }
  998.  
  999. #endif    /* __cplusplus    */
  1000.  
  1001. #if defined(__MWERKS__) && GENERATING68K
  1002. #pragma pointers_in_A0
  1003. #endif
  1004.  
  1005. #if PRAGMA_ALIGN_SUPPORTED
  1006. #pragma options align=reset
  1007. #endif
  1008.     
  1009. #endif    /* __OPENTPTMODULE__    */
  1010.